<?php

/*
 * Created on 2013-2-6
 *
 */
include_once dirname(__FILE__) . "/basedao.php";
include_once dirname(__FILE__) . "/../objects/topic.php";
include_once dirname(__FILE__) . "/../objects/web_topic.php";

class TopicsDAO extends BaseDAO {

	/**
	 * 添加帖子
	 */
	public function insertTopic($topic) {
		$this->getConn()->query("LOCK TABLES topics WRITE");
		$sqlstr = "INSERT INTO topics(topic_user_id, topic_title, topic_content, topic_datetime, topic_topictype_id) VALUES(?,?,?,?,?)";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("isssi", $topic->getTopicUserId(), $topic->getTopicTitle(), $topic->getTopicContent(), $topic->getTopicDatetime(), $topic->getTopicTopicTypeId());
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}

	/**
	 * 修改帖子
	 */
	public function updateTopic($topic) {
		$this->getConn()->query("LOCK TABLES topics WRITE");
		$sqlstr = "UPDATE topics SET topic_title=?, topic_content=?, topic_click=?, topic_topictype_id=?, topic_set_top_flag=? WHERE topic_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("ssiii", $topic->getTopicTitle(), $topic->getTopicContent(), $topic->getTopicClick(), $topic->getTopicSetTopFlag(), $topic->getTopicId());
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}

	/**
	 * 删除帖子
	 */
	public function deleteTopic($topic_id) {
		$this->getConn()->query("LOCK TABLES topics WRITE");
		$sqlstr = "DELETE FROM topics WHERE topic_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i", $topic_id);
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}

	/**
	 * 根据ID获取一个帖子
	 */
	public function getTopicById($topic_id) {
		$this->getConn()->query("LOCK TABLES topics READ");
		$sqlstr = "SELECT topic_id, topic_user_id, topic_title, topic_content, topic_click, topic_datetime, topic_topictype_id, topic_set_top_flag FROM topics WHERE topic_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i", $topic_id);
		$stmt->execute();
		$stmt->bind_result($topic_id, $topic_user_id, $topic_title, $topic_content, $topic_click, $topic_datetime, $topic_topictype_id, $topic_set_top_flag);
		$stmt->fetch();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		$topic = new Topic();
		$topic->setTopicId($topic_id);
		$topic->setTopicUserId($topic_user_id);
		$topic->setTopicTitle($topic_title);
		$topic->setTopicContent($topic_content);
		$topic->setTopicClick($topic_click);
		$topic->setTopicDatetime($topic_datetime);
		$topic->setTopicTopicTypeId($topic_topictype_id);
		$topic->setTopicTopFlag($topic_set_top_flag);
		return $topic;
	}

	/**
	 * 获取用户的所有帖子
	 */
	public function getTopicsByUser($user_id) {
		$topics = array();
		$this->getConn()->query("LOCK TABLES topics READ");
		$sqlstr = "SELECT topic_id, topic_user_id, topic_title, topic_content, topic_click, topic_datetime, topic_topictype_id, topic_set_top_flag FROM topics WHERE topic_user_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i", $user_id);
		$stmt->execute();
		$stmt->bind_result($topic_id, $topic_user_id, $topic_title, $topic_content, $topic_click, $topic_datetime, $topic_topictype_id, $topic_set_top_flag);
		while ($stmt->fetch()) {
			$topic = new Topic();
			$topic->setTopicId($topic_id);
			$topic->setTopicUserId($topic_user_id);
			$topic->setTopicTitle($topic_title);
			$topic->setTopicContent($topic_content);
			$topic->setTopicClick($topic_click);
			$topic->setTopicDatetime($topic_datetime);
			$topic->setTopicTopicTypeId($topic_topictype_id);
			$topic->setTopicTopFlag($topic_set_top_flag);
			array_push($topics, $topic);
		}
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		return $topics;
	}

	/**
	 * 获取某分类的所有帖子
	 */
	public function getTopicsByTopicType($topictype_id) {
		$topics = array();
		$this->getConn()->query("LOCK TABLES topics READ");
		$sqlstr = "SELECT topic_id, topic_user_id, topic_title, topic_content, topic_click, topic_datetime, topic_topictype_id, topic_set_top_flag FROM topics WHERE topic_topictype_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i", $topictype_id);
		$stmt->execute();
		$stmt->bind_result($topic_id, $topic_user_id, $topic_title, $topic_content, $topic_click, $topic_datetime, $topic_topictype_id, $topic_set_top_flag);
		while ($stmt->fetch()) {
			$topic = new Topic();
			$topic->setTopicId($topic_id);
			$topic->setTopicUserId($topic_user_id);
			$topic->setTopicTitle($topic_title);
			$topic->setTopicContent($topic_content);
			$topic->setTopicClick($topic_click);
			$topic->setTopicDatetime($topic_datetime);
			$topic->setTopicTopicTypeId($topic_topictype_id);
			$topic->setTopicTopFlag($topic_set_top_flag);
			array_push($topics, $topic);
		}
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		return $topics;
	}

	/**
	 * 修改帖子分类
	 * @param type $old_topictype_id
	 * @param type $new_topictype_id
	 */
	public function reType($old_topictype_id, $new_topictype_id) {
		$this->getConn()->query("LOCK TABLES topics WRITE");
		$sqlstr = "UPDATE topics SET topic_topictype_id=? WHERE topic_topictype_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("ii", $old_topictype_id, $new_topictype_id);
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}

	/**
	 * 根据ID获取一个用于在页面中显示的帖子
	 */
	public function getWebTopicById($topic_id) {
		$this->getConn()->query("LOCK TABLES topics READ");
		$sqlstr = "SELECT T.topic_id, T.topic_user_id, T.topic_title, T.topic_content, T.topic_click, T.topic_datetime, T.topic_topictype_id, T.topic_set_top_flag, U.user_name AS topic_user_name, TY.topictype_name" .
				"FROM topics AS T " .
				"LEFT OUTER JOIN users AS U ON U.user_id = T.topic_user_id " .
				"LEFT OUTER JOIN topictypes AS TY ON TY.topictype_id = topic_topictype_id " .
				"WHERE topic_user_id=?".
				" ORDER BY topic_datetime DESC";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i", $topic_id);
		$stmt->execute();
		$stmt->bind_result($topic_id, $topic_user_id, $topic_title, $topic_content, $topic_click, $topic_datetime, $topic_topictype_id, $topic_set_top_flag, $topic_user_name, $topic_topictype_name);
		$stmt->fetch();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		$topic = new TopicEx();
		$topic->setTopicId($topic_id);
		$topic->setTopicUserId($topic_user_id);
		$topic->setTopicTitle($topic_title);
		$topic->setTopicContent($topic_content);
		$topic->setTopicClick($topic_click);
		$topic->setTopicDatetime($topic_datetime);
		$topic->setTopicTopicTypeId($topic_topictype_id);
		$topic->setTopicTopFlag($topic_set_top_flag);
		$topic->setTopicUserName($topic_user_name);
		$topic->setTopicTopicTypeName($topic_topictype_name);
		return $topic;
	}

	/**
	 * 获取用户的用于在页面中显示的所有帖子
	 */
	public function getWebTopicsByUser($user_id) {
		$topics = array();
		$this->getConn()->query("LOCK TABLES topics READ");
		$sqlstr = "SELECT T.topic_id, T.topic_user_id, T.topic_title, T.topic_content, T.topic_click, T.topic_datetime, T.topic_topictype_id, T.topic_set_top_flag, U.user_name AS topic_user_name, TY.topictype_name" .
				"FROM topics AS T " .
				"LEFT OUTER JOIN users AS U ON U.user_id = T.topic_user_id " .
				"LEFT OUTER JOIN topictypes AS TY ON TY.topictype_id = topic_topictype_id " .
				"WHERE topic_user_id=?" .
				" ORDER BY topic_datetime DESC";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i", $user_id);
		$stmt->execute();
		$stmt->bind_result($topic_id, $topic_user_id, $topic_title, $topic_content, $topic_click, $topic_datetime, $topic_topictype_id, $topic_set_top_flag, $topic_user_name, $topic_topictype_name);
		while ($stmt->fetch()) {
			$topic = new TopicEx();
			$topic->setTopicId($topic_id);
			$topic->setTopicUserId($topic_user_id);
			$topic->setTopicTitle($topic_title);
			$topic->setTopicContent($topic_content);
			$topic->setTopicClick($topic_click);
			$topic->setTopicDatetime($topic_datetime);
			$topic->setTopicTopicTypeId($topic_topictype_id);
			$topic->setTopicTopFlag($topic_set_top_flag);
			$topic->setTopicUserName($topic_user_name);
			$topic->setTopicTopicTypeName($topic_topictype_name);
			array_push($topics, $topic);
		}
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		return $topics;
	}

	/**
	 * 根据分类取得用于在页面显示的帖子
	 * @param type $topic_type_id
	 */
	public function getWebTopicsByTopicType($topic_type_id) {
		$topics = array();
		$this->getConn()->query("LOCK TABLES topics READ");
		$sqlstr = "SELECT T.topic_id, T.topic_user_id, T.topic_title, T.topic_content, T.topic_click, T.topic_datetime, T.topic_topictype_id, T.topic_set_top_flag, U.user_name AS topic_user_name, TY.topictype_name" .
				"FROM topics AS T " .
				"LEFT OUTER JOIN users AS U ON U.user_id = T.topic_user_id " .
				"LEFT OUTER JOIN topictypes AS TY ON TY.topictype_id = topic_topictype_id " .
				"WHERE topic_topictype_id=?" .
				" ORDER BY topic_datetime DESC";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i", $topictype_id);
		$stmt->execute();
		$stmt->bind_result($topic_id, $topic_user_id, $topic_title, $topic_content, $topic_click, $topic_datetime, $topic_topictype_id, $topic_set_top_flag, $topic_user_name, $topic_topictype_name);
		while ($stmt->fetch()) {
			$topic = new TopicEx();
			$topic->setTopicId($topic_id);
			$topic->setTopicUserId($topic_user_id);
			$topic->setTopicTitle($topic_title);
			$topic->setTopicContent($topic_content);
			$topic->setTopicClick($topic_click);
			$topic->setTopicDatetime($topic_datetime);
			$topic->setTopicTopicTypeId($topic_topictype_id);
			$topic->setTopicTopFlag($topic_set_top_flag);
			$topic->setTopicUserName($topic_user_name);
			$topic->setTopicTopicTypeName($topic_topictype_name);
			array_push($topics, $topic);
		}
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		return $topics;
	}

	
	/**
	 * 取用于显示的所有帖子
	 * @param type $page_index
	 * @param type $page_size
	 * @return array
	 */
	public function getAllTopics($page_index=1,$page_size=10) {
		$topics = array();
		$this->getConn()->query("LOCK TABLES topics, users, topic_types READ");
		$sqlstr = "SELECT T.topic_id, T.topic_user_id, T.topic_title, T.topic_content, T.topic_click, T.topic_datetime, T.topic_topictype_id, T.topic_set_top_flag, U.user_name AS topic_user_name, TY.topictype_name " .
				"FROM topics AS T " .
				"LEFT OUTER JOIN users AS U ON U.user_id = T.topic_user_id " .
				"LEFT OUTER JOIN topictypes AS TY ON TY.topictype_id = T.topic_topictype_id ".
				"ORDER BY T.topic_datetime DESC";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->execute();
		$stmt->bind_result($topic_id, $topic_user_id, $topic_title, $topic_content, $topic_click, $topic_datetime, $topic_topictype_id, $topic_set_top_flag, $topic_user_name, $topic_topictype_name);
		while ($stmt->fetch()) {
			$topic = new TopicEx();
			$topic->setTopicId($topic_id);
			$topic->setTopicUserId($topic_user_id);
			$topic->setTopicTitle($topic_title);
			$topic->setTopicContent($topic_content);
			$topic->setTopicClick($topic_click);
			$topic->setTopicDatetime($topic_datetime);
			$topic->setTopicTopicTypeId($topic_topictype_id);
			$topic->setTopicTopFlag($topic_set_top_flag);
			$topic->setTopicUserName($topic_user_name);
			$topic->setTopicTopicTypeName($topic_topictype_name);
			array_push($topics, $topic);
		}
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		return $topics;
	}
}

?>
